<template>
  <el-container class="home-container">
    <!--头部-->
    <el-header>
      <div>
        <img class="home-logo" src="../assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <div>
        <span class="home-user-name">{{ userName }},你好</span>
        <el-button type="info" @click="loginOut">退出</el-button>
      </div>

    </el-header>
    <!--页面主体-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!--隐藏菜单-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--侧边栏菜单-->
        <el-menu
          :default-active="activePath"
          :router="true"
          :collapse="isCollapse"
          :unique-opened="true"
          :collapse-transition="false"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF">
          <!--一级菜单-->
          <el-submenu :index="'/'+item.path" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item
              :index="'/'+children.path"
              v-for="children in item.children"
              :key="children.id"
              @click="saveNavState('/'+children.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ children.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧内容-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: "Home",
    data(){
      return{
        menuList: [],
        iconsObj: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-tijikongjian',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao',
        },
        isCollapse:false,
        activePath: '',
        userName: ''
      }
    },
    created(){
      this.userName = window.sessionStorage.getItem('userName');
      this.getMenuList();
      this.activePath = window.sessionStorage.getItem('activePath');
    },
    methods: {
      loginOut() {
        window.sessionStorage.clear();
        this.$router.push('/login');
      },
      // 获取所有的菜单
      async getMenuList(){
        const {data: res} = await this.$http.get('menus');
        // console.log(res);
        if (res.meta.status !== 200){
          this.$message.error(res.meta.msg);
          return;
        }
        this.menuList = res.data;
        /*for (let i in res.data){
          let item = res.data[i];
          this.iconsObj[item.id] = `iconfont icon-${item.path}`;
          for (let j in item.children) {
            let item2 = item.children[j];
            this.iconsObj[item2.id] = `iconfont icon-${item2.path}`;
          }
        }*/

      },
      // 左侧菜单折叠与展开
      toggleCollapse(){
        this.isCollapse = !this.isCollapse;
      },
      // 保存链接的激活状态
      saveNavState(activePath){
        window.sessionStorage.setItem('activePath', activePath);
        this.activePath = activePath;
      }
    }
  }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .home-logo{
    width: 30px;
    margin-left: 20px;
  }

  .home-user-name{
    font-size: 14px;
    margin-right: 20px;
  }

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #ffffff;
    font-size: 20px;

    > div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    color: #333;
    .el-menu{
      border-right: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
    color: #333;
    position: relative;
    overflow: hidden;
  }

  .iconfont{
    margin-right: 10px;
  }

  .toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #ffffff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
